/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_manager.htm
 *
 * Zhiqim Manager is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager.action;

import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsIntegerValue;
import org.zhiqim.httpd.validate.ones.IsLen;
import org.zhiqim.httpd.validate.ones.IsSelect;
import org.zhiqim.httpd.validate.onex.IsInteger;
import org.zhiqim.kernel.json.Jsons;
import org.zhiqim.manager.ZmrConstants;
import org.zhiqim.manager.dao.ZmrOperatorDao;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.manager.dbo.ZmrRole;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;

/**
 * 角色管理
 *
 * @version v1.0.0 @author zouzhigang 2015-5-29 新建与整理
 */
public class RoleAction extends StdSwitchAction implements ZmrConstants
{
    @Override
    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsInteger("roleId", "请选择一个角色"));
    }

    @Override
    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsInteger("roleId", "请选择一个角色"));
        request.addValidate(new IsLen("roleName", "角色名称不能为空且不能超过32个汉字", 1, 32));
        request.addValidate(new IsSelect("roleValid", "请选择角色状态"));
        request.addValidate(new IsIntegerValue("roleSeq", "角色排序必须是[0, 999999]范围的非负整数", 0, 999999));
    }

    @Override
    protected void list(HttpRequest request) throws Exception
    {
        List<ZmrRole> list = ORM.table().list(ZmrRole.class, new Selector().addOrderbyAsc("roleSeq"));
        request.setAttribute("list", list);
    }

    @Override
    protected void insert(HttpRequest request) throws Exception
    {
        ZmrRole item = request.getParameter(ZmrRole.class);
        ORM.table().insert(item);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加操作员角色", Jsons.toString(item));
    }

    @Override
    protected void modify(HttpRequest request) throws Exception
    {
        long roleId = request.getParameterLong("roleId");
        ZmrRole item = ORM.table().item(ZmrRole.class, roleId);
        if (item == null)
        {
            request.returnHistory("角色不存在，请选择正确的角色");
            return;
        }
        
        request.setAttribute("item", item);
    }

    @Override
    protected void update(HttpRequest request) throws Exception
    {
        ZmrRole item = request.getParameter(ZmrRole.class);
        ORM.table().update(item);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "修改操作员角色", Jsons.toString(item));
    }

    @Override
    protected void delete(HttpRequest request) throws Exception
    {
        long roleId = request.getParameterLong("roleId");
        ZmrRole role = ORM.table().item(ZmrRole.class, roleId);
        if (role == null)
        {
            request.returnHistory("请选择一个有效的角色");
            return;
        }
        
        if (role.getRoleType() == 1)
        {
            request.returnHistory("系统默认角色不允许删除");
            return;
        }
        
        if (ORM.table().count(ZmrOperator.class, new Selector().addMustLike("operatorRole", roleId)) > 0)
        {
            request.returnHistory("该角色已有操作员，请删除角色成员后再删除本角色");
            return;
        }
        
        ORM.table().delete(ZmrRole.class, roleId);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "删除操作员角色", Jsons.toString("roleId", roleId));
    }
}